package com.wm.wmim.common.dto;

import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author weiming
 * @date 2025/3/5 16:54
 * @description
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {

    private int pageNum;

    private int pageSize;

    private long total;

    private List<T> result;


    public static <T> PageResult<T> fromPageInfo(PageInfo<T> pageInfo) {
        if (pageInfo == null) {
            throw new IllegalArgumentException("PageInfo cannot be null");
        }
        List<T> resultList = pageInfo.getList();
        if (resultList == null) {
            resultList = Collections.emptyList();
        }
        return PageResult.<T>builder()
                .pageNum(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .total(pageInfo.getTotal())
                .result(resultList).build();
    }

    public static <T> PageResult<T> fromPageInfo(PageInfo pageInfo, List<T> resultList) {
        if (pageInfo == null) {
            throw new IllegalArgumentException("PageInfo cannot be null");
        }
        return PageResult.<T>builder()
                .pageNum(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .total(pageInfo.getTotal())
                .result(resultList).build();
    }

    public static <T> PageResult<T> fromPageResult(PageResult pageResult, List<T> resultList) {
        if (pageResult == null) {
            throw new IllegalArgumentException("PageInfo cannot be null");
        }
        return PageResult.<T>builder()
                .pageNum(pageResult.getPageNum())
                .pageSize(pageResult.getPageSize())
                .total(pageResult.getTotal())
                .result(resultList).build();
    }
}
